package com.macaline.controller.menu;

import com.macaline.bean.Result;
import com.macaline.entity.Permission;
import com.macaline.service.perssion.PermissionService;
import com.macaline.service.perssion.RolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "用户权限相关接口")
@Controller
@RequestMapping("/permisson")
public class PermissionContrller {

    @Resource
    PermissionService permissionService;

    @Resource
    RolePermissionService rolePermissionService;

    @ResponseBody
    @ApiOperation(value = "添加权限", produces = "application/json")
    @RequestMapping(value = "addPermisson", method = RequestMethod.POST)
    public Object addPermisson(Permission permission){
        return permissionService.addPermisson(permission);
    }

    @ResponseBody
    @ApiOperation(value = "通过角色ID获取该角色所有拥有的权限")
    @ApiImplicitParam(name = "roleId", value = "查询的角色的ID")
    @RequestMapping(value = "getPermissionByRoleId", method = RequestMethod.POST)
    public Object getPermissionByRoleId(
            HttpServletRequest request,
            @RequestParam(value = "", required = false, defaultValue = "") int roleId){
        Result result = rolePermissionService.getPermissionByRoleId(roleId);
        List<Permission> list = permissionService.getPermissionListByIds((List) result.getData());
        return list;
    }

    @ResponseBody
    @ApiOperation(value = "获取所有权限信息")
    @RequestMapping(value = "getPermissionList", method = RequestMethod.POST)
    public Object getAllPermissionList(){
        return permissionService.getAllPermissionList();
    }

}
